﻿//----------------------------------------------------

#region 命名空间

//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Linq;
using WinnerCMS.BLL.PageFactory;
using WinnerCMS.CacheStorager;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// 业务逻辑类 WinnerCMS_Digg 的摘要说明。
    /// </summary>
    public class DiggManage : MarshalByRefObject
    {
        private static readonly DiggDal MyDal = new DiggDal();

        #region 添加修改

        /// <summary>
        /// 添加修改
        /// </summary>
        /// <param name="contentId"></param>
        /// <param name="type"></param>
        [CacheRemove("DiggManage.AjaxGetDigo_{@p0}")]
        public Result Add(int contentId, string type)
        {
            Digg model = new Digg
            {
                ContentId = contentId
            };

            DiggDiary ddModel = new DiggDiary
            {
                IP = HttpClient.GetIP()
            };

            model.DiggDiarys.Add(ddModel);

            if (RightsCheck.IsLogined)
            {
                ddModel.IsReg = true;
                ddModel.Name = UserManage.Current.UserName;
                model.LastUserName = UserManage.Current.UserName;
            }
            else
            {
                ddModel.IsReg = false;
                ddModel.Name = "游客";
                model.LastUserName = "游客";
            }
            Result res = new Result();
            if (MyDal.CheckPosted(contentId, ddModel.IP))
            {
                res.Errors = "您已经投过了。";

                return res;
            }

            if (type == "Good")
            {
                ddModel.IsAgree = true;
                MyDal.Save(model, 1);
            }
            else
            {
                ddModel.IsAgree = false;
                MyDal.Save(model, 0);
            }

            ddModel.IP = HttpClient.GetIP();
            ddModel.CheckInDate = DateTime.Now;

            return res;
        }

        #endregion

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        public void Delete(string ids)
        {
            int[] ints =
                (from x in ids.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) select int.Parse(x)).ToArray();
            MyDal.Delete(ints);
            ints.ForEach(x =>
            {
                CMSCaches.Context.Remove(string.Format("DiggManage.AjaxGetDigo_{0}", x));
            });
        }


        /// <summary>
        /// 取得Digg数据
        /// </summary>
        /// <param name="contentId"></param>
        [Cache("DiggManage.AjaxGetDigo_{@p0}")]
        public Digg AjaxGetDigo(int contentId)
        {
            Digg model = MyDal.GetDigo(contentId);
            if (model == null)
            {
                return new Digg();
            }
            return model;
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="modelId"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Digg> BindList(byte modelId, PagingParam pp)
        {
            var list = MyDal.BindList(pp);
            list.ForEach(x => x.Core.Link = StaticFilePath.ContentShowUrl(x.Core));
            return list;
        }
    }
}